package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.MerchantSecretPolicy;
import com.turing.post.organization.util.MerchantSecretPolicyDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "organization.impl.MerchantSecretPolicyDaoImpl")
@Remote
@Local
public class MerchantSecretPolicyDaoImpl implements MerchantSecretPolicyDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantSecretPolicy> getMerchantSecretPolicyByBusiness(
			MerchantSecretPolicy mfp) {
		Query query = _em
				.createNativeQuery(
						"select t.* from MerchantSecretPolicy t where "
								+ "t.MerchantNo = ?1 and t.Status = '1' "
								+ "and (t.MrchBranchNo = ?2 or t.MrchBranchNo is null) "
								+ "and (t.CardFaceId = ?3 or t.CardFaceId is null) "
								+ "and (t.CardClassicId = ?4 or t.CardClassicId is null) "
								+ "and (t.StartCardSeqNo <= ?5 or t.StartCardSeqNo is null) "
								+ "and (t.EndCardSeqNo >= ?5 or t.EndCardSeqNo is null) "
								+ "and (t.StartDate <= ?6 or t.StartDate is null) "
								+ "and (t.EndDate >= ?6 or t.EndDate is null) "
								+ "and (t.StartAmount <= ?7 or t.StartAmount = 0) "
								+ "and (t.EndAmount >= ?7 or t.EndAmount = 0) "
								+ "order by t.CREATETIME desc",
						MerchantSecretPolicy.class);

		query.setParameter(1, mfp.getMerchantNo());
		query.setParameter(2, mfp.getMrchBranchNo());
		query.setParameter(3, mfp.getCardFaceId());
		query.setParameter(4, mfp.getCardClassicId());
		query.setParameter(5, mfp.getStartCardSeqNo());
		query.setParameter(6, mfp.getStartDate());
		query.setParameter(7, mfp.getStartAmount());

		return query.getResultList();
	}

	@Override
	public MerchantSecretPolicy getMerchantSecretPolicyByPolicyId(
			BigInteger mspid) {
		return _em.find(MerchantSecretPolicy.class, mspid);
	}

	@Override
	public void insert(MerchantSecretPolicy mfp)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, mfp);
	}

	@Override
	public void update(MerchantSecretPolicy mfp)
			throws PCPSApplicationException {
		GeneralDao.update(_em, mfp);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantSecretPolicy> getMerchantSecretPolicyByBusinessPage(
			PCPSWebInput webInput, String MerchantNo, String CardFaceId,
			String CardClassicId, String Status, Integer page) {
		if (webInput == null)
			return null;
		String issueCompanyNo = webInput.getOperBranchNo().substring(0, 6);

		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from MerchantSecretPolicy where 1=1 "
				+ " and (?1 is null or CardFaceId=?1)"
				+ " and (?2 is null or CardClassicId=?2)";
		if (MerchantSecretPolicy.STATUS_WAIT_AUDITING.equals(Status)) {
			sql += " and (Status='"
					+ MerchantSecretPolicy.STATUS_INVALID_AUDITING
					+ "' or Status='"
					+ MerchantSecretPolicy.STATUS_VALID_AUDITING + "')";
		} else {
			if(!PCPSUtil.isEmpty(Status)){
				sql += " and Status='"+Status+"'";
			}
		}
		if (PCPSUtil.isEmpty(MerchantNo)) {
			sql = sql
					+ " and MerchantNo in(select MerchantNo from Merchant where IssueCompanyNo=?3)";
		} else {
			sql = sql
					+ " and MerchantNo in(select MerchantNo from Merchant where IssueCompanyNo=?3 and MerchantNo like ?4) order by CREATETIME desc";
		}
		Query query = _em.createNativeQuery(sql, MerchantSecretPolicy.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, CardFaceId);
		query.setParameter(2, CardClassicId);
		query.setParameter(3, issueCompanyNo);
		query.setParameter(4, "%" + MerchantNo + "%");
		return query.getResultList();
	}

	@Override
	public BigInteger getMerchantSecretPolicyByBusinessCount(
			PCPSWebInput webInput, String MerchantNo, String CardFaceId,
			String CardClassicId, String Status) {
		if (webInput == null)
			return BigInteger.ZERO;
		String issueCompanyNo = webInput.getOperBranchNo().substring(0, 6);
		String sql = "select nvl(count(*),0) from MerchantSecretPolicy t where 1=1"
				+ " and (?1 is null or CardFaceId=?1)"
				+ " and (?2 is null or CardClassicId=?2)";
		if (MerchantSecretPolicy.STATUS_WAIT_AUDITING.equals(Status)) {
			sql += " and (Status='"
					+ MerchantSecretPolicy.STATUS_INVALID_AUDITING
					+ "' or Status='"
					+ MerchantSecretPolicy.STATUS_VALID_AUDITING + "')";
		} else {
			if(!PCPSUtil.isEmpty(Status)){
				sql += " and Status='"+Status+"'";
			}
		}
		if (PCPSUtil.isEmpty(MerchantNo)) {
			sql = sql
					+ " and MerchantNo in(select MerchantNo from Merchant where IssueCompanyNo=?3)";
		} else {
			sql = sql
					+ " and MerchantNo in (select MerchantNo from Merchant where IssueCompanyNo=?3 and MerchantNo like ?4)";
		}
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, CardFaceId);
		query.setParameter(2, CardClassicId);
		query.setParameter(3, issueCompanyNo);
		query.setParameter(4, "%" + MerchantNo + "%");

		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void deleteMerchantSecretPolicy(MerchantSecretPolicy msp)
			throws PCPSApplicationException {
		GeneralDao.remove(_em, msp);
	}
}